/*
 * GuideFragment.java
 * business
 *
 * Created by ChenTao on 2017/2/28.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.guide;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.ui.adapter.GuideAdapter;
import com.yidiandao.business.ui.location.LocationActivity;
import com.yidiandao.business.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/** guide界面 */
public class GuideFragment extends BaseFragment implements GuideContract.View {

    private GuideContract.Presenter mPresenter;

    private ViewPager mViewPager;

    private LinearLayout mLlDotNormal;

    private ImageView mIvDotSelect;

    private int[] pagerResIds = new int[]{R.mipmap.ic_guide1, R.mipmap.ic_guide2};

    private List<View> mGuideImageViews = new ArrayList<>();

    private TextView mTvEnter;

    private int pointSpace;

    public static GuideFragment getInstance() {
        return new GuideFragment();
    }

    @Override
    public void setPresenter(GuideContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return GuideFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    protected void initViews() {
        mViewPager = (ViewPager) mRootView.findViewById(R.id.view_pager);
        mLlDotNormal = (LinearLayout) mRootView.findViewById(R.id.ll_dot_normal);
        mIvDotSelect = (ImageView) mRootView.findViewById(R.id.iv_dot_select);

        mPresenter.start();
        initPagers();
        initEvent();
    }

    private void initEvent() {
        mIvDotSelect
                .getViewTreeObserver()
                .addOnGlobalLayoutListener(
                        new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                //两点间距
                                pointSpace =
                                        mLlDotNormal.getChildAt(1).getLeft()
                                                - mLlDotNormal.getChildAt(0).getLeft();
                                mIvDotSelect
                                        .getViewTreeObserver()
                                        .removeGlobalOnLayoutListener(this);
                            }
                        });

        mViewPager.addOnPageChangeListener(
                new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(
                            int position, float positionOffset, int positionOffsetPixels) {
                        //点的左间距
                        int leftMargin = (int) (pointSpace * (position + positionOffset));
                        RelativeLayout.LayoutParams params =
                                (RelativeLayout.LayoutParams) mIvDotSelect.getLayoutParams();
                        params.leftMargin = leftMargin;
                        mIvDotSelect.setLayoutParams(params);
                    }

                    @Override
                    public void onPageSelected(int position) {
                        mLlDotNormal.setVisibility(
                                position == mGuideImageViews.size() - 1 ? View.GONE : View.VISIBLE);
                        mIvDotSelect.setVisibility(
                                position == mGuideImageViews.size() - 1 ? View.GONE : View.VISIBLE);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });

        mTvEnter.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        toLocationActivity();
                        mActivity.finish();
                    }
                });
    }

    @Override
    public void toLocationActivity() {
        LocationActivity.actionStart(mActivity, false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_guide;
    }

    private void initPagers() {

        //添加页面
        for (int pagerResId : pagerResIds) {
            ImageView iv = new ImageView(mActivity);
            iv.setImageResource(pagerResId);
            //            ImageLoader.loadLocalImage(mActivity, iv, pagerResId);
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            mGuideImageViews.add(iv);
        }

        View view = View.inflate(UIUtils.getContext(), R.layout.page_guide_last, null);
        mTvEnter = (TextView) view.findViewById(R.id.tv_enter);
        mGuideImageViews.add(view);

        //添加未选中的点
        for (int k = 0; k < mGuideImageViews.size(); k++) {
            ImageView dotView = new ImageView(mActivity);
            dotView.setBackgroundResource(R.drawable.guide_dot_normal);
            LinearLayout.LayoutParams params =
                    new LinearLayout.LayoutParams(
                            (int) UIUtils.getDimens(R.dimen.dimen_5),
                            (int) UIUtils.getDimens(R.dimen.dimen_5));
            if (k != mGuideImageViews.size() - 1) {
                params.rightMargin = (int) UIUtils.getDimens(R.dimen.dimen_8);
            }
            mLlDotNormal.addView(dotView, params);
        }

        //选中的点
        RelativeLayout.LayoutParams params =
                new RelativeLayout.LayoutParams(
                        (int) UIUtils.getDimens(R.dimen.dimen_5),
                        (int) UIUtils.getDimens(R.dimen.dimen_5));
        mIvDotSelect.setLayoutParams(params);
        //添加数据
        GuideAdapter guideAdapter = new GuideAdapter(mGuideImageViews);
        mViewPager.setAdapter(guideAdapter);
    }
}
